/* 
 * File:   Circle.cpp
 * Author: mauro
 * 
 * Created on 9 de septiembre de 2014, 17:49
 */

#include "Circle.h"

Circle::Circle(ObjFactory *objFactory, bool flagStatic, float x, float y, int scale, float mass) {
    SBSLog* myLogFile;
    myLogFile->getInstance(MY_LOG_MODE);

    if (scale <= 0) {
        myLogFile->writeInLog(2, "[2] El radio del circulo es invalido (Radio no positivo)\n");
    }

    Dot* dot = new Dot(x, y);
    this->dot = dot;
    this->scale = scale;
    float sca = scale * 0.01;
    b2Vec2 size(0, 0);
    Object* object = objFactory->getObject(flagStatic, dot->dotPixelsToWorld(), &size, sca, mass, 0, 0);
    this->object = object;
    this->objFactory = objFactory;

    myLogFile->writeInLog(0, "[0] Se ha creado un circulo exitosamente\n");

    //para dibujar la imagen
    //    renderer = objFactory->getRender();
    //    ballImage = new SBSImage();
    //    ballImage->loadMedia("Img/ball.png", &renderer);
}

void Circle::draw(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
    SBSLog* myLogFile;
    myLogFile->getInstance(MY_LOG_MODE);

    Object *obj = this->object;

    float posX = obj->getBody()->GetPosition().x;
    float posY = obj->getBody()->GetPosition().y;

    this->dot->setPos(posX, posY);
    b2Vec2* posForSDL = this->dot->dotWorldToPixels();
    bool ok = obj->getSkin()->makeCircle(posForSDL, this->scale, this->getAngle(), r, g, b, a);

    if (!ok) {
        myLogFile->writeInLog(2, "[2] Ha ocurrido un error mientras se intentaba dibujar el circulo\n");
    }

    //    SDL_Rect* ballSettings = new SDL_Rect();
    //
    //    SDL_Texture* mTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, this->objFactory->getUnitWidth(), this->objFactory->getUnitHeight());
    //    SDL_SetRenderTarget(renderer, mTexture);
    //    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
    //    SDL_SetTextureBlendMode(mTexture, SDL_BLENDMODE_BLEND);
    //    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xAA, SDL_BLENDMODE_NONE);
    //    SDL_RenderClear(renderer);
    //    ballSettings->w = ((this->scale)*2);
    //    ballSettings->h = ((this->scale)*2);
    //    ballSettings->x = (posForSDL->x) - ((ballSettings->w) / 2);
    //    ballSettings->y = (posForSDL->y) - ((ballSettings->h) / 2);
    //    SDL_RenderCopyEx(renderer, ballImage->getTexture(), NULL, ballSettings, this->object->getBody()->GetAngle()* 57, NULL, SDL_FLIP_NONE);
    //    SDL_SetRenderTarget(renderer, NULL);
    //
    //    SDL_RenderCopyEx(renderer, mTexture, NULL, NULL, 0, NULL, SDL_FLIP_NONE);
    //    SDL_DestroyTexture(mTexture);
    //    mTexture = NULL;
    //
    //    delete ballSettings;

}

b2Vec2 Circle::getPosition() {
    return this->object->getPosition();
}

float Circle::getAngle() {
    return this->object->getAngle();
}

float Circle::getAngularVelocity() {
    return this->object->getAngularVelocity();
}

void Circle::setAngularVelocity(float velocity) {
    this->object->setAngularVelocity(velocity);
}

Circle::~Circle() {
    delete this->object;
    delete this->dot;
//    printf("Destructor de Circle \n");
}

